﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        if (!checkForMissingFields())
        {
            ForumLibrary.Data.ForumManager fm = new ForumLibrary.Data.ForumWebManager();
            string registerValue = RegisterDS.Select().ToString();

            if (registerValue.Contains("not available"))
                FieldProblemLabel.Text = registerValue;
            else
            {
                fm.subscribe(null, UsernameTextBox.Text, false);
                Response.Redirect("~/Login.aspx");
            }
        }

    }
    protected void RegisterDS_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        string moderatorPassword = ForumLibrary.Data.ForumManager.ModeratorPassword;
        if (string.IsNullOrEmpty(ModeratorPasswordTextBox.Text) || 
            !ModeratorPasswordTextBox.Text.Equals(moderatorPassword))
        {
            e.InputParameters["isModerator"] = false;
        }
        else if (ModeratorPasswordTextBox.Text.Equals(moderatorPassword))
            e.InputParameters["isModerator"] = true;
    }

    private bool checkForMissingFields()
    {
        FieldProblemLabel.Text = "";
        List<string> missingFields = new List<string>();

        if (string.IsNullOrEmpty(FirstNameTextBox.Text)) missingFields.Add("First Name");
        if (string.IsNullOrEmpty(LastNameTextBox.Text)) missingFields.Add("Last Name");
        if (string.IsNullOrEmpty(UsernameTextBox.Text)) missingFields.Add("Username");
        if (string.IsNullOrEmpty(PasswordTextBox.Text)) missingFields.Add("Password");
        if (string.IsNullOrEmpty(ConfirmPasswordTextBox.Text)) missingFields.Add("ConfirmPassword");
        if (string.IsNullOrEmpty(PhoneNumberTextBox.Text)) missingFields.Add("PhoneNumber");
        if (string.IsNullOrEmpty(EmailTextBox.Text)) missingFields.Add("Email");

        if (missingFields.Count > 0)
        {
            for (int i = 0; i < missingFields.Count-1; i++)
            {
                FieldProblemLabel.Text += missingFields.ElementAt(i) + ", ";
            }
            FieldProblemLabel.Text += missingFields.ElementAt(missingFields.Count - 1);
            FieldProblemLabel.Text += " fields are required";
            FieldProblemLabel.Visible = true;
        }
        if (missingFields.Count > 0)
            return true;
        return false;
    }
}